package com.controller.ssjf;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.common.utils.base.Const;
import com.controller.base.BaseController;
import com.entity.Product;
import com.entity.User;
import com.service.ProductService;
import com.service.UserService;

@Controller
@RequestMapping("/ssjf")
public class SSJFProductController extends BaseController<Object>{
	
	@Autowired
	private ProductService productService;
	@Autowired
	private UserService userService;
	
	/**
	 * 展示产品列表
	 * @param o
	 * @param page
	 * @return
	 */
	@RequestMapping("/pro/list.dat")
	@ResponseBody
	public AjaxRes prolist(Product o, Page<Product> page){
		AjaxRes res = new AjaxRes();
		Page<Product> products = productService.findByPage(o, page);
		res.setSucceed(products, Const.DATA_SUCCEED);
		return res;
	}
	
	/**
	 * 展示推荐产品列表
	 * @param o
	 * @param page
	 * @return
	 */
	@RequestMapping("page/recommend.html")
	public String recommend(Model model)throws Exception{
		model.addAttribute("isRecommend", true);
		return "ssjf/pages/state2";
	}
	
	
	/**
	 * 展示产品详细信息
	 * @param o
	 * @param page
	 * @return
	 */
	@RequestMapping("page/product.html")
	public String product(Model model, String prevPage ,Product p)throws Exception{
		p = productService.find(p).get(0);
		getRequest().getSession().setAttribute("prevPage", prevPage);
		model.addAttribute("pro", p);
		return "ssjf/pages/product";
	}
	
	/**
	 * 上传产品
	 * @param map
	 * @return
	 */
	@RequestMapping("/upload/sub")
	@ResponseBody
	public AjaxRes up(@RequestBody Map<String,Object> map){
		AjaxRes res = new AjaxRes();
		String json = JSONArray.toJSONString(map.get("obj"));
		Product product = JSON.parseArray(json, Product.class).get(0);
		User u = (User) getRequest().getSession().getAttribute("loggedUser");
		product.setPublisher(u.getUserName());
		product.setProductState(1);
		//添加默认封面
		if(product.getCover() == null || product.getCover() == ""){
			product.setCover("upload/file/empty-img.png");
		}
		productService.insert(product);
		res.setSucceedMsg(Const.SAVE_SUCCEED);
		return res;
	}
	
	@RequestMapping("page/state.html")
	public String state(Model model)throws Exception{
		List<String> companys = userService.findCompany(null);
		model.addAttribute("companys", companys);
		return "ssjf/pages/state";
	}
	
	@RequestMapping("pro/state.dat")
	@ResponseBody
	public AjaxRes stateData(Product pro, Page<Product> page)throws Exception{
		AjaxRes res = new AjaxRes();
		Page<Product> pros = productService.findByPage(pro, page);
		res.setSucceed(pros, Const.DATA_SUCCEED);
		return res;
	}

}
